<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<!-- <meta name="viewport" content="width=device-width, initial-scale=1.0"> -->
		<meta http-equiv="X-UA-Compatible" content="ie=edge">
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
		<style type="text/css">
			body,
			html,
			#allmap {
				width: 100%;
				height: 100%;
				overflow: hidden;
				margin: 0;
				font-family: "微软雅黑";
			}
		</style>
		<script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=s01fm4Rbo6494DuwCOkSfM5AHU7DAS6a&s=1"></script>
		<title>南阳高速定位报警</title>
		</style>
	</head>

	<body>
		<div id="allmap" style="display:block;height:60%;width:100%">
		</div>
		<div>
			<p style="margin-right:30px;margin-left:10px;margin-top:30px;font-size:18px;">当前位置:&nbsp<span class="num1"></span></p>
			<p style="margin-right:30px;margin-left:10px;margin-top:10px;color:blue;"><span class="num2"></span></p>
			<p style="margin-right:30px;margin-left:10px;margin-top:20px;color:blue;"><span class="num3"></span></p>
			<a href="javascript:;" id="myid" style="width:60%;margin-right:50px;margin-left:50px;margin-top:60px;display:block;
            margin:0 auto;background-color: #446fa6;border: none; color: white;padding: 10px 20px;text-align: center;
            text-decoration: none;font-size: 16px;" onclick="saveSmsSeat()">发送位置</a>
		</div>

		<!-- <a href="tel:13828172679">13622178579</a> -->

	</body>

	<script src="./Package/js/jquery-1.7.2.js"></script>
	<script src="./Package/js/base64.js"></script>
	<script>
		var str = '';
		var londata = '';
		var latdata = '';
		var map = new BMap.Map("allmap");
		map.centerAndZoom(new BMap.Point(111.803648, 32.76717), 10);
		map.enableScrollWheelZoom(true);

		getLocation();

		function showPosition(position) {
			var x = gcj02tobd09(position.coords.longitude, position.coords.latitude)[0];
			var y = gcj02tobd09(position.coords.longitude, position.coords.latitude)[1];
			var point = new BMap.Point(x, y);
			map.centerAndZoom(point, 12);
			var marker = new BMap.Marker(point); // 创建标注
			map.addOverlay(marker); // 将标注添加到地图中

			var label = new BMap.Label("当前位置", {
				offset: new BMap.Size(20, -10)
			});
			marker.setLabel(label);
			getroad(x, y)
		}
		getroad(111.79862,32.793012)
		function getroad(lon, lat) {
			$.ajax({
				type: 'post',
				url: 'http://192.168.1.107:9910/dev-api/event/manager/locationAll/getLocation',
				dataType: 'json',
				data: JSON.stringify({
					"lon": lon,
					"lat": lat,
					"city": "南阳市"
				}),
				contentType: "application/json",
				headers: {
					Authorization: localStorage.getItem('token')
				},
				success: function(data) {
					console.log(data)
					if(data.code == 200) {
						$('.num1').text(data.data.street + data.data.kilometers);
						str = data.data;
						londata = lon;
						latdata = lat;
					}else {
						$('.num2').text("当前定位不在南阳高速辖区内,暂无辖区信息");
						$('.num3').text("如需报警请拨打110");
					}
				},
				error: function(err) {
					$('.num2').text("当前定位不在南阳高速辖区内,暂无辖区信息");
					$('.num3').text("如需报警请拨打110");
				}
			})
		}
		function saveSmsSeat() {
			$.ajax({
				type: 'post',
				url: 'http://192.168.1.107:9910/dev-api/event/eventOrder/messageLocationOrderSave',
				dataType: 'json',
				data: JSON.stringify({
					"lon":londata,
					 "lat":latdata,
					 "kil":str.kilometers.replace('k','').replace('K',''),
					 "meter":0,
					 "sectionName": str.speed,
					 "orderId": Base64.decode(getQueryString('pi'))
				}),
				contentType: "application/json",
				headers: {
					Authorization: localStorage.getItem('token')
				},
				success: function(data) {
					console.log(data, '经纬度')
					if(data.code == 200) {
						alert('发送成功')
					}else{
						alert(data.msg)
					}
				},
				error: function(err) {
					// 失败的回调
				}
			})
		}
		
		function getQueryString(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
			var r = window.location.search.substr(1).match(reg);
			if(r != null) return unescape(r[2]);
			return null;
		}
		function gcj02tobd09(lng, lat) {
			var that = this
			var x_PI = 3.14159265358979324 * 3000.0 / 180.0;
			var PI = 3.1415926535897932384626;
			var a = 6378245.0;
			var ee = 0.00669342162296594323;
			var z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_PI);
			var theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_PI);
			var bd_lng = z * Math.cos(theta) + 0.0065;
			var bd_lat = z * Math.sin(theta) + 0.006;
			return [bd_lng, bd_lat]
		}

		function getLocation() {
			if(navigator.geolocation) {
				navigator.geolocation.getCurrentPosition(showPosition, showError);
			} else {
				alert("浏览器不支持地理定位。");
			}
		}

		function showError(error) {
			switch(error.code) {
				case error.PERMISSION_DENIED:
					alert("定位失败,用户拒绝请求地理定位");
					break;
				case error.POSITION_UNAVAILABLE:
					alert("定位失败,位置信息是不可用");
					break;
				case error.TIMEOUT:
					alert("定位失败,请求获取用户位置超时");
					break;
				case error.UNKNOWN_ERROR:
					alert("定位失败,定位系统失效");
					break;
			}
		}
	</script>

</html>